package net.m510.autosurfer.mhbk;

import java.net.URI;
import java.util.logging.Logger;

import net.m510.autosurfer.common.DocumentGetProcess;
import net.m510.autosurfer.common.exception.InvalidDocumentException;
import net.m510.autosurfer.core.utils.AnalyzeUtils;

import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author mgoto
 *
 */
public class MhbkLogout extends DocumentGetProcess {
	private static final Logger log = Logger.getLogger(MhbkLogout.class.getName());

	public MhbkLogout() {
		this(null,null);
	}
	public MhbkLogout(CloseableHttpClient client, HttpClientContext context) {
		super(client, context);
		super.log = log;
	}
	@Override
	protected URI getUri(Document doc) {
		Elements es = doc.select("img[alt=ログアウト]");
		if ( es.isEmpty() ) throw new InvalidDocumentException("ログアウトのimgタグが見つかりません", doc);
		log.fine("logout element="+es.first().parent());
		Element postkey = doc.getElementById("POSTKEY");
		log.fine("postkey="+postkey);
		String s = es.first().parent().attr("href");
		URI uri = AnalyzeUtils.resolveURIByPath(context, s);
		log.fine("logout uri="+uri);
		return uri;
	}
}